package util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisUtil {
    public static int MAX_ACTIVE = 5;//最大连接数
    public static int MAX_IDLE = 200;//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例
    public static int MAX_WAIT = 10000;//等待可用连接的最大时间
    public static int TIMEOUT = 10000;
    public static int RETRY_NUM = 5;

    private static String ADDR = "192.168.94.130";
    private static int PORT = 6379;

    private static JedisPool jedisPool;


    static{
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(MAX_ACTIVE);
        jedisPoolConfig.setMaxIdle(MAX_IDLE);
        jedisPoolConfig.setMaxWaitMillis(MAX_WAIT);
        jedisPool = new JedisPool(jedisPoolConfig,ADDR,PORT);
    }


    //开大redis
    public static Jedis openJedis(){
        return jedisPool.getResource();
    }

    public static void closeJedis(Jedis jedis){
        if (jedis != null){
                jedis.close();
        }
    }
}
